home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * Copyright (C) 1991, Silicon Graphics, Inc.
- * All Rights Reserved.
- */
- /* Standard Disclaimer ...
- This source code provided as is with no warranty as to
- suitability for any purpose .... enjoy */
-
- /* Amesh.c Real-Time FFT visualization of an audio stream
-
- Credits:
- Original Concept and implementation
- David S. Maynard
- FFT code
- Bruce Karsh
- Wheel View concept and implementation
- Chris Perry
- Track Ball emulation code by
- Thant Tessman
-
- I hope you enjoy this program.
-
- Please let me know if you make any cool additions
- David S. Maynard
- Silicon Graphics Computer Systems
- dsm@sgi.com
- February 25, 1992
- */
-
- #include <stdio.h>
- #include <gl.h>
- #include <device.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <sys/types.h>
- #include <sys/dir.h>
- #include <string.h>
-
- #include <audio.h>
-
- /* ORDER defines the number of frequency bands plotted */
- /* ORDER is the number of points along the x-axis */
- #define ORDER 64
-
- /* DURATION is the number of power spectrums plpotted */
- /* DURATION is the number of points along the y-axis */
- #define DURATION 25
-
- /* BUFCHUNKSIZE is the default number of samples transformed per frame */
- /* BUFCHUNKSIZE must be at least twice ORDER to avoid aliasing in the */
- /* frequncy domain */
- #define BUFCHUNKSIZE 128
- int curfftsize = BUFCHUNKSIZE;
-
- /* Basic program loop is as follows
- while (true)
- {
- read all waiting audio samples from input buffer
- perform an FFT on the last "curfftsize" samples
- calculate the power in each of the lowest "ORDER" frequency bands
- add this power spectrum to the front of the rolling carpet
- plot the rolling carpet of power spectrums
- }
- */
-
- #define RGBTIMEFACT 3.2
- #define LMTIMEFACT 1.35
- #define MMTIMEFACT 1.35
-
- #define MAXBUFFERSIZE 60000
- #define ABUFFERSIZE 40000
- float v0[3] = { -1.0, -1.0, -1.0 } ;
- float v1[3] = { -1.0, -1.0, 1.0 } ;
- float v2[3] = { -1.0, 1.0, 1.0 } ;
- float v3[3] = { -1.0, 1.0, -1.0 } ;
- float v4[3] = { 1.0, -1.0, -1.0 } ;
- float v5[3] = { 1.0, -1.0, 1.0 } ;
- float v6[3] = { 1.0, 1.0, 1.0 } ;
- float v7[3] = { 1.0, 1.0, -1.0 } ;
-
- #define MAXDURATION (DURATION<<1)
- #define TWOPI 6.2832
- float amp[MAXDURATION][ORDER]; /* independent variable */
-
- float power[DURATION][ORDER]; /* vertices of rolling carpet */
- float ident_matrix[4][4] = {
- {1.0, 0.0, 0.0, 0.0},
- {0.0, 1.0, 0.0, 0.0},
- {0.0, 0.0, 1.0, 0.0},
- {0.0, 0.0, 0.0, 1.0},
- };
- float xlogpos[ORDER]; /* table to store pre-computed x coordinates log scale */
-
-
- char *myname;
- ALport port;
- ALconfig conf;
- int curlinewidth = 2;
- int candoRGB = 0;
- int mono = 0;
- int DB = 1; /* if true use double buffering */
- int framecount =0;
- int zbuf = 0; /* if true use zbuffering */
-
- /* use 16 bit audio samples ( no cheesy 8 bit mu-law please) */
-
- typedef short SAMPLE;
-
-
-
- SAMPLE tbuf[2*MAXBUFFERSIZE];
-
- extern float spectrum( SAMPLE * , int , float *, int, float );
-
-
- float dist=0.0, d_dist=0.0;
- Angle azim=0, inc=700, twist=0;
- Angle d_azim=0, d_inc=0;
-
- short mx, my, omx, omy, nmx, nmy;
- long origx, origy, sizex, sizey;
-
- int y;
- long mode, playmode, prevmode;
- int active=TRUE;
- long gid;
- #define TIMELINES 1
- #define FREQLINES 2
- #define AMPLINES 4
-
- int low_res, low_tmp;
-
- int function = 0;
- #define REORIENT 1
- #define REDISTANCE 2
- #define PREPAREAIFF 3
-
- #define DTHETA 0.6
- #define PWRMAX 100.0
-
- int idur=0;
- float maxpwr = PWRMAX; /* maxpwr used for automatic gain control */
- float pwr = 1.0;
- int facing = 0, facingz = 0, buggy = 0, buggyview = 0;
-
- float p1[ 3], p2[ 3], tv1[ 3], tv2[ 3];
- int dotmesh;
-
- short mute; /* if TRUE then don't play sound out */
- short logfreq = 0; /* IF TRUE USE LOG SCALE FOR FREQUENCY AXIS */
- short tmeshmode = 0; /* IF TRUE USE Tmesh drawing mode */
- #define FIRSTTIME 1
- #define INPORT 2
- #define OUTPORT 4
-
-
- /* pop-up menu id's */
- long mainmenu, linemenu, samplemenu, wheelmenu, drawmenu, fftmenu;
-
- /* typedef void (* ALerrfunc) (long, const char*, ...); /*XXXX*/
-
- static void drawpower();
- static void mappower();
- static void drawaxis();
-
-
- static void generate(void)
- {
- int id, io;
- for (id=0;id<MAXDURATION;id++)
- for (io=0;io<ORDER;io++)
- amp[id][io] = 0.0;
- }
-
- static void place(void)
- {
- int nxt = idur, id, io;
-
- for (id=0;id<DURATION;id++) {
- for (io=0;io<ORDER;io++) {
- power[id][io] = amp[nxt][io]; /* y */
- }
- nxt--;
- if (nxt < 0 ) nxt = MAXDURATION-1;
- }
- }
-
-
- static void drawbuggy()
- {
-
- float gravity = -0.01;
- float bikelength = 3;
- float t1ypos, t2ypos, t1yprev, t2yprev;
-
- static float thetaa = 0.0;
-
- p1 [0] = ORDER/2 + facing;
- if (logfreq) p1[0] = xlogpos[ORDER/2 + facing] ;
- p1 [2] = -DURATION/2 + facingz;
- p2 [0] = ORDER/2 + facing;
- if (logfreq) p2[0] = xlogpos[ ORDER/2 + facing] ;
- p2 [2] = -DURATION/2 +facingz - bikelength;
-
- t1ypos = power[ -(int)p1[2]][ORDER/2 + facing]/maxpwr;
- t2ypos = power[ -(int)p2[2]][ORDER/2 + facing]/maxpwr;
-
- t1yprev = power[ -(int)p1[2]-1][ORDER/2 + facing]/maxpwr;
- t2yprev = power[ -(int)p2[2]-1][ORDER/2 + facing]/maxpwr;
-
- tv1 [1] += ( p1[ 1] > t1ypos) ? gravity : t1ypos - t1yprev;
- tv2 [1] += ( p2[ 1] > t2ypos) ? gravity : t2ypos - t2yprev;
-
- p1[ 1] += tv1[ 1];
- p2[ 1] += tv2[ 1];
-
- if ( p1[ 1] < 0.0) {
- p1[ 1] = 0.0;
- tv1[ 1] *= - 0.2;
- }
- if ( p2[ 1] < 0.0) {
- p2[ 1] = 0.0;
- tv2[ 1] *= -0.2;
- }
-
- if ( buggy && !buggyview) {
- pushmatrix();
- scale(1.0, 1.0/20.0, (float)DURATION/(float)ORDER);
- rotate(900, 'y');
- pushmatrix();
- translate((-p1[2] + 1) * ((float)ORDER/(float)DURATION),
- 1 + p1[1]*20.0, p1[0]-0.05);
-
- RGBcolor(0xff,0xff,0xff);
- circi( 0, 0, 1);
- translate( 0, 0, 0.1);
- circi( 0, 0, 1);
- popmatrix();
-
- if ( p1[ 1] > 0.5) thetaa+= DTHETA/2.0;
- else thetaa += DTHETA;
- if ( thetaa > TWOPI) thetaa-=TWOPI;
-
- RGBcolor(0xff,0,0);
- move((-p1[2] + 1) * ((float)ORDER/(float)DURATION) + 0.9 *
- sinf(thetaa), p1[1]*20.0 + 1 - 0.9 * cosf(thetaa), p1[0]);
- draw((-p1[2] + 1) * ((float)ORDER/(float)DURATION) - 0.9 *
- sinf(thetaa), p1[1]*20.0 + 1 + 0.9 * cosf(thetaa), p1[0]);
- move((-p1[2] + 1) * ((float)ORDER/(float)DURATION) - 0.9 *
- sinf(thetaa + TWOPI/4.0), p1[1]*20.0 + 1 + 0.9 *
- cosf(thetaa + TWOPI/4.0), p1[0]);
- draw((-p1[2] + 1) * ((float)ORDER/(float)DURATION) + 0.9 *
- sinf(thetaa + TWOPI/4.0), p1[1]*20.0 + 1 - 0.9 *
- cosf(thetaa + TWOPI/4.0), p1[0]);
-
- popmatrix();
- }
- }
-
- static void draw_scene(void)
- {
- RGBcolor(00,00,00);
- clear();
- if (zbuf) zclear();
- pushmatrix();
- if (buggyview) lookat (p1[ 0] - ORDER/2, ORDER/DURATION * (- p1[ 2] - 14),
- 20.0*p1[ 1] + 2, p1[ 0] - ORDER/2,
- ORDER/DURATION * (- p1[ 2] - 14) - DURATION,
- (20.0*p1[ 1] + 2) / 4.0, 1800);
- else polarview(dist+d_dist, azim+d_azim, inc+d_inc, twist);
- rotate(900, 'x');
- translate(-ORDER/2.0, 0.0, ORDER/2.0);
- scale(1.0,20.0, ORDER/DURATION);
-
- place();
- drawpower();
-
- if ( buggy || buggyview) drawbuggy();
- popmatrix();
-
- if (DB) swapbuffers(); else gsync();
- }
-
-
- static void drawpower()
- {
- float dx,dz,xp,zp,vert[3];
- float invmax, thresh;
- float *p;
- int ORD2 = ORDER >> 1,
- bright,
- id,io,
- rc,gc,bc;
-
- dx = 1.0;
- invmax = 1.0/maxpwr;
- thresh = maxpwr/50.0;
- framecount++;
- linewidth(curlinewidth);
- if (mono) {
- rc = 0x80;
- gc = 0x80;
- bc = 0x80;
- cpack(0xffffff);
- }
- if (tmeshmode) {
- dz = 1.0;
- zp = -DURATION+dz;
- cpack(0xffffff);
- if (!buggyview) /* Draw tmesh back to front */
- for(id=DURATION-1;id>=1; id--) {
- xp = -1.0;
- p = &power[id][0];
- bgntmesh();
- for (io=0;io<ORDER;io++,p++) {
- if (!mono) {
- if (io <= ORD2)
- {
- rc = 0xff*(ORD2-io)/ORDER;
- gc = 0xff*(io)/ORDER;
- bc = 0;}
- else
- {
- rc = 0;
- gc = 0xff*(ORDER-io)/ORDER;
- bc = (0x80*io)/ORDER;
- }
-
- if ( *p > thresh)
- {
- bright = (int)(0x5f* ((*p))*invmax) + 0x10;
- cpack ( (rc+bright&0xff) |
- (((gc+bright)<<8)&0xff00) |
- (((bc+bright) << 16)&0xff0000));
- }
- else cpack(0x222222);
- }
- if( logfreq) xp = xlogpos[io]; else xp += dx;
- vert[0] = xp; vert[1] = *p*invmax; vert[2] = zp;
- v3f(vert);
- vert[0] = xp; vert[1] = power[id-1][io]*invmax; vert[2] = zp+dz;
- v3f(vert);
- }
- endtmesh();
- zp += dz;
- }
- else /* buggyview, draw from front to back: we are facing the front */
- {
- dz = -1.0;
- zp = 0;
- for(id=0 ;id<=DURATION-1; id++) {
- xp = -1.0;
- p = &power[id][0];
- bgntmesh();
- for (io=0;io<ORDER;io++,p++) {
- if (!mono) {
- if (io <= ORD2)
- {
- rc = 0xff*(ORD2-io)/ORDER;
- gc = 0xff*(io)/ORDER;
- bc = 0;}
- else
- {
- rc = 0;
- gc = 0xff*(ORDER-io)/ORDER;
- bc = (0x80*io)/ORDER;
- }
-
- if ( *p > thresh)
- {
- bright = (int)(0x5f* ((*p))*invmax) + 0x10;
- cpack ( (rc+bright&0xff) |
- (((gc+bright)<<8)&0xff00) |
- (((bc+bright) << 16)&0xff0000));
- }
- else cpack(0x111111);
- }
- if( logfreq) xp = xlogpos[io]; else xp += dx;
- vert[0] = xp; vert[1] = *p*invmax; vert[2] = zp;
- v3f(vert);
- vert[0] = xp; vert[1] = power[id+1][io]*invmax; vert[2] = zp+dz;
- v3f(vert);
- }
- endtmesh();
- zp += dz;
- }
- }
- }
- if (mode & TIMELINES) {
- xp = -1.0;
- dz = -1.0;
- for(io=0;io<ORDER; io++) {
- zp = 0.0;
- if (!mono) {
- rc = (io <= ORD2) ? (0xff*(ORD2-io)/ORDER) : 0;
- gc = (io <= ORD2) ? (0xff*(io)/ORDER) : 0xff*(ORDER-io)/ORDER;
- bc = (io <= ORD2) ? 0 : (0x80*io)/ORDER;
- }
- if( logfreq) xp = xlogpos[io]; else xp += dx;
- bgnline();
- vert[0] = xp; vert[1] = 0.0; vert[2] = 0.0;
- v3f(vert);
- for (id=0;id<DURATION;id++) {
- if (!mono) {
- bright = (int)(0x3f* ((power[id][io]))*invmax) + 0x40;
- cpack ( (rc+bright&0xff) |
- (((gc+bright)<<8)&0xff00) |
- (((bc+bright) << 16)&0xff0000));
- }
- if (tmeshmode) cpack(0x000000);
- vert[0] = xp; vert[1] = power[id][io]*invmax; vert[2] = zp;
- v3f(vert);
- zp += dz;
- }
- endline();
- }
- }
- if (mode & FREQLINES) {
- dz = 1.0;
- zp = -DURATION+dz;
- cpack(0xffffff);
- for(id=DURATION-1;id>=0; id--) {
- xp = -1.0;
- p = &power[id][0];
- bgnline();
- vert[0] = 0.0; vert[1] = ((*p))*invmax; vert[2] = zp;
- v3f(vert);
- for (io=0;io<ORDER;io++,p++) {
- if (!mono) {
- if (io <= ORD2)
- {
- rc = 0xff*(ORD2-io)/ORDER;
- gc = 0xff*(io)/ORDER;
- bc = 0;}
- else
- {
- rc = 0;
- gc = 0xff*(ORDER-io)/ORDER;
- bc = (0x80*io)/ORDER;
- }
-
- bright = (int)(0x3f* ((*p))*invmax) + 0x40;
- cpack ( (rc+bright&0xff) |
- (((gc+bright)<<8)&0xff00) |
- (((bc+bright) << 16)&0xff0000));
- }
- if (tmeshmode) cpack(0x000000);
- if( logfreq) xp = xlogpos[io]; else xp += dx;
- vert[0] = xp; vert[1] = power[id][io]*invmax;
- v3f(vert);
- }
- endline();
- zp += dz;
- }
- }
- if (mode & AMPLINES) {
- xp = -1.0;
- dz = -1.0;
- for(io=0;io<ORDER; io++) {
- zp = 0.0;
- if (!mono) {
- rc = (io <= ORD2) ? (0xff*(ORD2-io)/ORDER) : 0;
- gc = (io <= ORD2) ? (0xff*(io)/ORDER) : 0xff*(ORDER-io)/ORDER;
- bc = (io <= ORD2) ? 0 : (0x80*io)/ORDER;
- }
- if (logfreq) xp = xlogpos[io]; else xp += dx;
- for (id=0;id<DURATION;id++) {
- if (!mono) {
- bright = (int)(0x3f* ((power[id][io]))*invmax) + 0x40;
- cpack ( (rc+bright&0xff) |
- (((gc+bright)<<8)&0xff00) |
- (((bc+bright) << 16)&0xff0000));
- }
- if (tmeshmode) cpack(0x000000);
- bgnline();
- vert[0]= xp; vert[1] = 0.0; vert[2] = zp;
- v3f(vert);
- vert[1] = power[id][io]*invmax;
- v3f(vert);
- endline();
- zp += dz;
- }
- }
- }
-
- }
-
-
- static void drawaxis()
- {
- move(0.0, 0.0, 0.0);
- cpack(0xff);
- draw(ORDER, 0.0, 0.0);
- move(0.0, 0.0, 0.0);
- cpack(0xff00);
- draw(0.0,.50, 0.0);
- move(0.0, 0.0, 0.0);
- cpack(0xff0000);
- draw(0.0, 0.0, -DURATION);
- }
-
- static void initialize(void)
- {
- int i;
- float xscale ;
- /* Initialsize xlogpos table */
- curfftsize = BUFCHUNKSIZE;
- tmeshmode = 0;
- logfreq = 0;
- xscale = ORDER*1.0 / flog(ORDER*1.0);
- for (i=0;i<ORDER;i++)
- xlogpos[i] = flog( (i+1)*1.0) * xscale;
- gid = winopen("Visual Audio");
-
- if (DB) doublebuffer();
- if(getgdesc(GD_BITS_NORM_SNG_RED) == 0) candoRGB = 0;
- else candoRGB = 1;
- if (!candoRGB) { fprintf(stderr, " Amesh requires RGB mode to run \n"); exit(1);}
- RGBmode();
- shademodel( GOURAUD);
- zbuffer(zbuf);
- gconfig();
- RGBcolor(0, 0, 0);
- clear();
- if(DB) { swapbuffers(); clear(); } else gsync();
- if (zbuf) zclear();
- qdevice(INPUTCHANGE);
- qdevice(REDRAW);
- qdevice(ESCKEY);
- qdevice(WINQUIT);
- qdevice(WINSHUT);
- qdevice(WINFREEZE);
- qdevice(WINTHAW);
- qdevice(LEFTMOUSE);
- qdevice(MIDDLEMOUSE);
- qdevice(RIGHTMOUSE);
- qdevice(RKEY); /* replay current sample */
- qdevice(LEFTARROWKEY); /* following four move wheel */
- qdevice(RIGHTARROWKEY);
- qdevice(UPARROWKEY);
- qdevice(DOWNARROWKEY);
- qdevice(BKEY); /* Toggle wheel on/off */
- qdevice(VKEY); /* Toggle view from/of wheel */
- qdevice(SKEY); /* Resynchronize without popup menu */
-
- p1[ 0] = p1[ 1] = p1[ 2] = p2[ 0] = p2[ 1]= p2[2] = v1[ 0] = v1[ 1] =
- v1[ 2] = v2[ 0] = v2[ 1]= v2[ 2] = 0.0;
- facingz = -DURATION/2+3;
- generate();
- dist = DURATION + 70.0;
-
- perspective(500, 5.0/4.0, 0, dist);
-
- qreset();
- qenter(REDRAW, (short)winget());
- }
-
- /* extern "C" ALerrfunc ALseterrorhandler(ALerrfunc efunc); /*XXXX*/
-
- static void efunc( long num, const char * ch, ...)
- {
- }
-
- /* func is given by logical or of FIRSTTIME, OUTPORT, and INPORT */
- static void open_audio(short func)
- {
- ALerrfunc origfunc;
- short badopen = 0;
-
- origfunc = ALseterrorhandler(efunc);
- if (func & FIRSTTIME) {
- conf = ALnewconfig();
- ALsetwidth(conf, AL_SAMPLE_16);
- ALsetchannels(conf, AL_MONO);
- port = 0;
- }
- if (func & INPORT) badopen = badopen ||
- ((port = ALopenport("audio_in", "r", conf)) == 0);
- if (badopen) {
- fprintf(stderr, "%s: could not open the necessary audio ports\n",
- myname);
- system("inform 'could not open the audio ports'");
- exit(1);
- }
- ALseterrorhandler(origfunc);
- }
- /* func is given by a logical or of OUTPORT and INPORT */
- static void close_audio(short func)
- {
- if ((func & INPORT) && port) { ALcloseport(port); port = 0; }
- }
-
-
-
-
- static void remake_lmenu(void)
- {
- long i;
-
- freepup(linemenu);
- linemenu = defpup("Line Widths %t| One %x11| Two %x12| Four %x14| Six %x16");
- for (i = 0; i < 4; i++)
- setpup(linemenu, i+1, PUP_BOX);
- switch (curlinewidth) {
- case 1: i = 1; break;
- case 2: i = 2; break;
- case 4: i = 3; break;
- case 6: i = 4; break;
- default: curlinewidth = 1; i = 1; break;
- }
- setpup(linemenu, i, PUP_CHECK);
- }
-
- static void remake_fftmenu(void)
- {
- long i;
- long buf [4] ;
- char menustr[400];
- char tmpstr[40];
- long inputfreq;
- float nyquistfreq;
- buf[0] = AL_INPUT_RATE;
- ALgetparams (AL_DEFAULT_DEVICE,buf,2);
- inputfreq = buf[1];
- freepup(fftmenu);
- menustr[0] = '\0';
- strcat(menustr, "FFT Size %t | 128 (0 to");
-
- nyquistfreq = 1.0*inputfreq*ORDER/128/1000;
- sprintf(tmpstr,"%5.1f KHz)", nyquistfreq);
- strcat(menustr,tmpstr);
- strcat(menustr," %x41| 256 (0 to");
-
- nyquistfreq = 1.0*inputfreq*ORDER/256/1000;
- sprintf(tmpstr,"%5.1f KHz)", nyquistfreq);
- strcat(menustr,tmpstr);
- strcat(menustr," %x42| 512 (0 to");
-
- nyquistfreq = 1.0*inputfreq*ORDER/512/1000;
- sprintf(tmpstr,"%5.1f KHz)", nyquistfreq);
- strcat(menustr,tmpstr);
- strcat(menustr," %x43| 1024 (0 to");
-
- nyquistfreq = 1.0*inputfreq*ORDER/1024/1000;
- sprintf(tmpstr,"%5.1f KHz)", nyquistfreq);
- strcat(menustr,tmpstr);
- strcat(menustr," %x44");
-
- /* fftmenu = defpup("FFT Size %t| 128 %x41| 256 %x42| 512 %x43| 1024 %x44"); */
- fftmenu = defpup(menustr);
- for (i = 0; i < 4; i++)
- setpup(fftmenu, i+1, PUP_BOX);
- switch (curfftsize) {
- case 128: i = 1; break;
- case 256: i = 2; break;
- case 512: i = 3; break;
- case 1024: i = 4; break;
- default: curfftsize = 128; i = 1; break;
- }
- setpup(fftmenu, i, PUP_CHECK);
- }
- static void remake_dmenu(void)
- {
- long i;
-
- freepup(drawmenu);
- drawmenu = defpup("Draw modes %t|Draw Time %x8|Draw Frequency %x9|Draw Amplitude %x10");
- for (i = 0; i < 3; i++)
- setpup(drawmenu, i+1, (mode & (TIMELINES << i)) ?
- PUP_CHECK : PUP_BOX);
- }
-
- static void remake_wmenu(void)
- {
- freepup(wheelmenu);
- wheelmenu = defpup("Wheel Options %t|Wheel %x5|Wheel View %x6");
- setpup(wheelmenu, 1, buggy ? PUP_CHECK : PUP_BOX);
- setpup(wheelmenu, 2, buggyview ? PUP_CHECK : PUP_BOX);
- }
-
-
- static void remake_mmenu(void)
- {
- if (mono) setpup(mainmenu, 3, PUP_BOX);
- else setpup(mainmenu, 3, PUP_CHECK);
-
- if (logfreq) setpup(mainmenu, 4, PUP_CHECK);
- else setpup(mainmenu, 4, PUP_BOX);
- if (tmeshmode) setpup(mainmenu, 5, PUP_CHECK);
- else setpup(mainmenu, 5, PUP_BOX);
-
- }
-
- static void remake_menus(void)
- {
- freepup(mainmenu);
- mainmenu = defpup("Visual Audio %t|Draw Modes %x0%m|Line Width %x0%m|Color %x2|Log Freq Scale %x30|Tmesh %x32|Wheel %x0%m|FFT Size %x40%m|Quit %x1", drawmenu, linemenu, wheelmenu, fftmenu);
- remake_mmenu();
- remake_fftmenu();
- }
-
- static void make_menus(void)
- {
- remake_lmenu();
- remake_wmenu();
- remake_dmenu();
- remake_fftmenu();
- remake_menus();
- }
-
- static int wait_till_thawed(void)
- {
- long dev;
- short val;
-
- close_audio(INPORT | OUTPORT);
- do {
- dev = qread(&val);
- } while (dev != WINQUIT && dev != WINSHUT && dev != WINTHAW);
- if (dev == WINQUIT || dev == WINSHUT) return TRUE;
- open_audio(INPORT | OUTPORT);
- qenter(REDRAW, (short)winget());
- return FALSE;
- }
-
- static void usage(void)
- {
- fprintf(stderr,"usage: %s \n",myname);
- system("inform 'usage: amesh '");
- exit(1);
- }
-
-
- void main( int argc, char **argv)
- {
- long xrot,yrot,zrot;
- long i, ntoread, dev;
- int nframe=0;
- short done = FALSE, stowed = FALSE, val;
- long nfilled;
-
- float time_per_frame = 0.0, timefact = 1.0, basetime = 0.0;
-
-
- for (i=0;i<8000;i++) tbuf[i] = (SAMPLE) ( (i*8)%256 - 128 );
- xrot = yrot = zrot = 0;
- dotmesh = 0;
-
-
- open_audio(FIRSTTIME | INPORT /* | OUTPORT */);
-
- initialize();
-
- idur = 0;
-
- /* if (port) ALsetfillpoint(port, (ALgetqueuesize(conf) - BUFCHUNKSIZE)); */
-
- playmode = 1;
- mode = FREQLINES;
- prevmode = mode;
- make_menus();
-
-
-
- while (!done) {
- draw_scene();
- idur++;
- if (idur >= MAXDURATION) idur = 0;
-
-
- nfilled = ALgetfilled(port);
- ntoread = nfilled > ABUFFERSIZE ? ABUFFERSIZE : nfilled;
-
-
- ALreadsamps(port, (void *) tbuf, ntoread);
- pwr = spectrum(tbuf+(ntoread-curfftsize),
- curfftsize, &[idur][0], ORDER, 0.0 );
- if (pwr < maxpwr) nframe++;
- if (nframe > DURATION) { nframe = 0; maxpwr *= 0.97;}
- if (pwr > maxpwr) { maxpwr = pwr; nframe = 0 ;}
-
- timefact = 1.0;
-
- /* Thant's input code */
- while(qtest() /* || (!function && low_tmp<2) */ ) {
- dev=qread(&val);
- switch(dev) {
- case INPUTCHANGE:
- active = val;
- break;
- case WINQUIT:
- case WINSHUT: done = TRUE; break;
- case WINFREEZE: stowed = TRUE; break;
- case ESCKEY :
- if (!val) done = TRUE;
- break;
- case SKEY:
- break;
- case VKEY:
- if (( val == 0) || (!buggy) ) break;
- buggyview = !buggyview;
- break;
- case LEFTMOUSE:
- mx = (short) getvaluator(MOUSEX);
- my = (short) getvaluator(MOUSEY);
- timefact *= LMTIMEFACT;
- if (val) {
- function=REDISTANCE;
- setvaluator(MOUSEX, mx,(short) origx+1,
- (short)(origx+sizex-1));
- setvaluator(MOUSEY, my,(short) origy+1,
- (short)(origy+sizey-1));
- omx = mx; omy = my;
- } else {
- function=0;
- setvaluator(MOUSEX, mx, 0, XMAXSCREEN);
- setvaluator(MOUSEY, my, 0, YMAXSCREEN);
- dist += d_dist; d_dist = 0.0;
- }
- break;
- case MIDDLEMOUSE:
- timefact *= MMTIMEFACT;
- mx = (short)getvaluator(MOUSEX);
- my = (short)getvaluator(MOUSEY);
- if (val) {
- function=REORIENT;
- setvaluator(MOUSEX, mx, (short)origx+1,
- (short)(origx+sizex-1));
- setvaluator(MOUSEY, my, (short)origy+1,
- (short)(origy+sizey-1));
- omx = mx; omy = my;
- } else {
- function=0;
- setvaluator(MOUSEX, mx, 0, XMAXSCREEN);
- setvaluator(MOUSEY, my, 0, YMAXSCREEN);
- azim += d_azim; d_azim = 0;
- inc += d_inc; d_inc = 0;
- }
- break;
- case RIGHTMOUSE:
- if (val) {
- long menval;
- /* in case the input rate has changed */
- remake_fftmenu();
- menval = dopup(mainmenu);
- switch (menval) {
- case -1: break;
- case 1: /* quit */
- done = 1;
- break;
- case 2: /* color */
- mono ^= 1;
- remake_mmenu();
- break;
- case 5: /* wheel toggle */
- buggy ^= 1;
- remake_wmenu();
- remake_menus();
- break;
- case 6: /* wheel view toggle */
- buggyview ^= 1;
- remake_wmenu();
- remake_menus();
- break;
- case 8: /* toggle time */
- mode ^= TIMELINES;
- goto fix_time_per_frame;
- case 9: /* toggle frequency */
- mode ^= FREQLINES;
- goto fix_time_per_frame;
- case 10: /* toggle amplitude */
- mode ^= AMPLINES;
- fix_time_per_frame:
- remake_dmenu(); /* also fixup menu */
- remake_menus();
- /* nothing yet */
- break;
- case 11: case 12: case 13: case 14:
- case 15: case 16: case 17: case 18: case 19:
- curlinewidth = (int)(menval-10);
- remake_lmenu();
- remake_menus();
- break;
- case 21:
- case 22:
- case 30: /* logfreq */
- logfreq ^= 1;
- remake_mmenu();
- break;
- case 32: /* Tmesh */
- if (tmeshmode) {tmeshmode=0; mode=prevmode; }
- else {
- prevmode = mode; tmeshmode = 1;
- mode = 0; mono =0;
- }
- remake_mmenu();
- remake_dmenu();
- break;
- case 41:
- curfftsize = 128;
- remake_fftmenu();
- break;
- case 42:
- curfftsize = 256;
- remake_fftmenu();
- break;
- case 43:
- curfftsize = 512;
- remake_fftmenu();
- break;
- case 44:
- curfftsize = 1024;
- remake_fftmenu();
- break;
- default:
- break;
- }
- }
- break;
- case REDRAW:
- reshapeviewport();
- getorigin(&origx, &origy);
- getsize(&sizex, &sizey);
- break;
- case LEFTARROWKEY:
- if ( val == 0) break;
- facing += 1;
- if ( facing + ORDER/2 >= ORDER) facing = -ORDER/2;
- break;
- case RIGHTARROWKEY:
- if ( val == 0) break;
- facing -= 1;
- if ( facing + ORDER/2 < 0) facing = ORDER/2 - 1;
- break;
- case UPARROWKEY:
- if ( val == 0) break;
- facingz += 1;
- if ( -DURATION/2 + facingz > 0) facingz = -DURATION/2 + 1;
- break;
- case DOWNARROWKEY:
- if ( val == 0) break;
- facingz -= 1;
- if ( -DURATION/2 + facingz <= -DURATION) facingz = DURATION/2 ;
- break;
- case BKEY:
- if ( val == 0) break;
- buggy = !buggy;
- break;
- }
- }
-
- switch(function) {
-
- case REORIENT:
- mx = (short)getvaluator(MOUSEX);
- my = (short)getvaluator(MOUSEY);
- d_azim = (short)((omx - mx) * 2 * XMAXSCREEN / sizex);
- d_inc = (short)((my - omy) * 2 * YMAXSCREEN / sizey);
- break;
-
- case REDISTANCE:
- mx = (short)getvaluator(MOUSEX);
- my = (short)getvaluator(MOUSEY);
- d_dist = ((omx - mx) + (my - omy)) * XMAXSCREEN /sizex;
- break;
- }
-
-
- if (stowed) {
- done = wait_till_thawed();
- stowed = FALSE;
- }
-
- }
- close_audio(INPORT /* | OUTPORT */);
- }
-
-